///
/// @ProjectName:    learning_rust
/// @Description:    
/// @File:           jz6.rs
/// @Author:         wujehy
/// @Email:          wujehy@qq.com
/// @Time:           2021-07-01 21:28
///

struct Solution{

}

impl Solution {
    fn new() -> Self {
        Solution{}
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param rotateArray int整型一维数组
     * @return int整型
     */
    #[allow(non_snake_case)]
    pub fn minNumberInRotateArray(&self, rotateArray: Vec<i32>) -> i32 {
        // write code here
        let arr = rotateArray.as_slice();
        if rotateArray.len() == 0  {
            return 0;
        }
        // 切片递归
        return self.minNumberInRotateArrayInner(arr);
    }
    #[allow(non_snake_case)]
    pub fn minNumberInRotateArrayInner(&self, rotateArray: &[i32]) -> i32{
        println!("current : {:?}" , rotateArray);
        // 只有一个 则是本身
        if rotateArray.len() == 1  {
            return rotateArray[0];
        }else if rotateArray.len() == 2  {
            // 两个取最小
            if rotateArray[0] <= rotateArray[1] {
                return rotateArray[0];
            }else {
                return rotateArray[1];
            }
        }else {
            // 超过3 个 进行二分 递归
            let end = rotateArray.len() ;
            let mid =end / 2  ;
            // println!(" mid = {} , end = {}" , mid , end);
            // println!("l = {} , r = {}" , rotateArray[mid] , rotateArray[end -1 ] );
            // println!("---");
            if rotateArray[mid] > rotateArray[end -1 ] {
                self.minNumberInRotateArrayInner(&rotateArray[mid..end])
            }else {
                self.minNumberInRotateArrayInner(&rotateArray[0..mid+1 ])
            }
        }
    }
}

#[test]
fn test6()
{
    let s = Solution::new();
    let array = vec![ 7,8,9,11,12 ,13 ,3 ,4,5,6];
    let expext = 3 ;
    let result = s.minNumberInRotateArray(array);
    assert_eq!(expext , result);
    println!("result : {} " , result);
}